package com.ywgt.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ywgt.system.mapper.*;
import com.ywgt.system.pojo.Consumer;
import com.ywgt.system.service.ConsumerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ywgt.system.vo.ConsumerVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yiping
 * @since 2021-01-22
 */
@Service
public class ConsumerServiceImpl extends ServiceImpl<ConsumerMapper, Consumer> implements ConsumerService {

    @Autowired
    private ConsumerMapper consumerMapper;


    @Override
    public Page<Consumer> findConsumerList(Long pageNum, Long pageSize, Consumer consumer) {
        Page<Consumer> page=new Page<>(pageNum,pageSize);
        Page<Consumer> consumerPage = consumerMapper.selectPage(page, getWrapper(consumer));
        return consumerPage;
    }

    @Override
    public boolean updateConsumer(Consumer consumer) {
        if(consumerMapper.updateById(consumer)==1){
            return true;
        }
        return false;
    }

    @Override
    public boolean add(ConsumerVO consumerVO) {
        Consumer consumer=new Consumer();
        BeanUtils.copyProperties(consumerVO,consumer);
        if (consumerMapper.insert(consumer)==1){
            return true;
        }
        return false;
    }

    public LambdaQueryWrapper<Consumer> getWrapper(Consumer consumer){
        LambdaQueryWrapper<Consumer> queryWrapper=new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(consumer.getName())){
            queryWrapper.like(Consumer::getName,consumer.getName());
        }
        if (!StringUtils.isEmpty(consumer.getContact())){
            queryWrapper.like(Consumer::getContact,consumer.getContact());
        }
        if (!StringUtils.isEmpty(consumer.getAddress())){
            queryWrapper.like(Consumer::getAddress,consumer.getAddress());
        }
        return queryWrapper;
    }
}
